package com.kuaiditu.assistant.ui.main;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.entity.TaskSquareEn;
import com.kuaiditu.assistant.service.LocationService;
import com.kuaiditu.assistant.util.JudgeExpressUtil;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import cn.iwgang.simplifyspan.SimplifySpanBuild;
import cn.iwgang.simplifyspan.unit.SpecialTextUnit;

/**
 * Created by Eenie on 2017/8/30.
 * Email: eeniework@gmail.com
 * Des:
 */

public class TaskAdapter extends BaseQuickAdapter<TaskSquareEn, BaseViewHolder> {

    private SimpleDateFormat mSimpleDateFormat;


    LocationService mLocationService;


    DecimalFormat    df   = new DecimalFormat("######0.00");

    public TaskAdapter(@Nullable List<TaskSquareEn> data, LocationService mLocationService) {
        super(R.layout.item_main_task, data);
        mSimpleDateFormat = new SimpleDateFormat("MM.dd");

        this.mLocationService = mLocationService;


    }

    @Override
    protected void convert(BaseViewHolder helper, TaskSquareEn item) {


        SimplifySpanBuild hintSpanBuild = new SimplifySpanBuild();
        hintSpanBuild.append(new SpecialTextUnit(mSimpleDateFormat.format(item.getOrder_startTime()), mContext.getResources().getColor(R.color.colorPrimary)))
                .append("至")
                .append(new SpecialTextUnit(mSimpleDateFormat.format(item.getOrder_endTime()), mContext.getResources().getColor(R.color.colorPrimary)))
                .append(",共")
                .append(new SpecialTextUnit(String.valueOf(item.getOrder_dayNum()), mContext.getResources().getColor(R.color.colorPrimary)))
                .append("天,预计")
                .append(new SpecialTextUnit(String.valueOf(item.getOrder_sendOrderNum()), mContext.getResources().getColor(R.color.colorPrimary)))
                .append("单/天");


//                String taskInfo = String.format("%s至%s,共%s天 预计%s单/天", ),
//                mSimpleDateFormat.format(item.getOrder_endTime()), item.getOrder_dayNum(), item.getOrder_sendOrderNum());


        String takeAddress = String.format("%s%s%s%s", item.getOrder_getProvinceName(),
                item.getOrder_getCityName(),
                item.getOrder_getDistrictName(),
                item.getOrder_getAddress());
        String sendAddressStart = String.format("%s%s%s%s%s", item.getOrder_sendProvinceName(), item.getOrder_sendCityName()
                , item.getOrder_sendDistrictName(),
                item.getOrder_sendAddress(), item.getOrder_sendStartAddress());
        String sendAddressEnd = String.format("%s%s%s%s%s", item.getOrder_sendProvinceName(), item.getOrder_sendCityName()
                , item.getOrder_sendDistrictName(),
                item.getOrder_sendAddress(), item.getOrder_sendEndAddress());


        TextView tvCom = helper.getView(R.id.tv_express_com);

        JudgeExpressUtil.analyze(item.getOrder_netsite(), tvCom);

        helper.addOnClickListener(R.id.btn_take_task);

        SimplifySpanBuild spanBuild = new SimplifySpanBuild();
        spanBuild.append(new SpecialTextUnit(sendAddressStart, Color.parseColor("#333333")))
                .append(new SpecialTextUnit(" 至 ", Color.parseColor("#999999")))
                .append(new SpecialTextUnit(item.getOrder_sendEndAddress(), Color.parseColor("#333333")));

        LatLng latLng = new LatLng(item.getOrder_getAddressLat(), item.getOrder_getAddressLng());
        LatLng latLng1 = mLocationService.getLatLng();
        String destance;
        if (latLng1 != null) {
            destance = df.format(DistanceUtil.getDistance(latLng1, latLng) / 1000);
        } else {
            destance = "--";
        }


        helper.setText(R.id.tv_task_price, String.format("￥%s", item.getOrder_depositCost()))
                .setText(R.id.tv_task_info, hintSpanBuild.build())
                .setText(R.id.tv_address_take, takeAddress)
                .setText(R.id.tv_address_send, spanBuild.build())
                .setText(R.id.tv_distance, String.format("距您%skm", destance))
                .setText(R.id.tv_range, String.format("范围%skm", df.format(item.getOrder_distance() / 1000)));
    }

}
